//首页
var List = function(){
    this.element = $('#list');
    this.cache = {
        linkType: null,
        cid: null,
        data: []
    }
}
List.prototype = {
    render: function(obj){
        var self = this;
        var title = this.element.find('input[name=title]').val();
        var server = this.element.find('input[name=link]').val();
        var type = this.element.find('input[name=type]').val();

        //统计提示出现的次数
        self.countTips();

        self.cache.linkType = type;

        if(title != '' && server != '' && type != ''){
            $.ajax({
                type: 'get',
                url: server,
                dataType: 'json',
                success:function(result){
                    self.cache.data = result.data;
                    self.buildNode();

                    $(function(){
                        setTimeout(function(){
                            $('#sub-title').html(title).parent().addClass('hd-logbox-list');
                            $('#hd-bak').show();
                        }, 1000);
                    })
                }
            });
        }
    },

    countTips: function(){
        var num = parseInt(localStorage.getItem('mt-mobile-view') || 0, 10);

        //10次后不显示提示
        if(num > 9){
            this.element.find('.list-tips').hide();
        }else{
            localStorage.setItem('mt-mobile-view', num + 1);
        }
    },

    /* build内容 */
    buildNode: function(data){
        var data = this.cache.data;
        var el = this.element.find('ul');
        var index = 0;
        var mobileDomain = this.cache.mobileDomain;
        var linkType = this.cache.linkType;

        for(var i=0,len=data.length;i<len;i++){
            var html = [];
            var link = '/share/detail/all/'+ data[i].share_id +'/#m'+ data[i].cover_id;
            var img = '<img src="'+ data[i].cover +'!240" src="" width="100%" />';
            
            if(i > 15){
                img = '<img class="lazyload" data-original="'+ data[i].cover +'!240" src="http://s.lovewith.me/static/images/blank.gif" width="100%" />';
            }

            if(linkType == 'share'){
                link = '/share/detail/all/'+ data[i].share_id +'/';
            }

            html.push(
                '<li>',
                    '<a href="', link ,'" class="s-link">',
                        img,
                    '</a>',
                '</li>'
            );

            el.eq(index).append(html.join(''));

            index++;

            if(index > 2){index = 0}
        }

        this.element.removeClass('p-loading');

        $('.lazyload').lazyload();
    },

    //解析url
    getParam: function(key){
        var val = location.href.match(new RegExp('[\?\&]' + key + '=([^\&]*)(\&?)', 'i'));
        return val ? val[1] : null;
    },

    //截取字符串
    cutstr: function(str,len){
        var str_length = 0;
        var str_len = 0;
        var str_cut = new String();
        var str_len = str.length;

        for(var i = 0; i<str_len; i++){
            a = str.charAt(i);
            str_length++;

            if(escape(a).length > 4) {
                //中文字符的长度经编码之后大于4
                str_length++;
            }

            str_cut = str_cut.concat(a);
            
            if(str_length>=len){
                str_cut = str_cut.concat("...");
                return str_cut;
            }
        }

        //如果给定字符串小于指定长度，则返回源字符串；
        if(str_length < len){
            return str;
        }
    }
};